package com.heima.leetcode.practice;

/**
 * @Classname E209
 * @Description leetcode 209. 长度最小的子数组
 * @Date 2025/10/14 9:28
 * @Author 勾新杰
 */
public class E209 {

    /**
     * 思想：滑动窗口
     * @param target 目标值
     * @param nums 数组
     * @return 最小长度
     */
    public int minSubArrayLen(int target, int[] nums) {
        int left = 0, right = 0, currSum = nums[0], minLength = nums.length + 1;
        while (right < nums.length) {
            while (currSum >= target) {
                minLength = Math.min(minLength, right - left + 1);
                currSum -= nums[left++];
            }
            right++;
            if (right < nums.length) currSum += nums[right];
        }
        return minLength == nums.length + 1 ? 0 : minLength;
    }
}
